package com.goodboys.service.impl;

import com.goodboys.base.entity.BaseReqBean;
import com.goodboys.base.service.impl.BaseServiceImpl;
import com.goodboys.entity.Comment;
import com.goodboys.entity.UserTag;
import com.goodboys.service.CommentService;
import com.goodboys.service.InfoService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by CJ on 2018-05-25.
 */
@Service("commentService")
public class CommentServiceImpl extends BaseServiceImpl<Comment> implements CommentService {

    private final Logger logger = LoggerFactory.getLogger(CommentServiceImpl.class);

    @Autowired
    private InfoService infoService;

    @Override
    public Map addComment(BaseReqBean baseReqBean) throws Exception {
        Map map = new HashMap();map.put("status", 0);
        if(baseReqBean != null && !StringUtils.isEmpty(baseReqBean.getId()) &&
                !StringUtils.isEmpty(baseReqBean.getContent()) && !StringUtils.isEmpty(baseReqBean.getUserId())){
            commentDao.insertSelective(convertToComment(baseReqBean));
            //返回该原始信息及评论
            //这里通过Detail查找
            map.put("status", 1);
            map.put("message",  infoService.findInfoDetail(baseReqBean));
        }else {
            map.put("message", "参数值存在为空，请重试。。。");
            logger.error("ERROR=====>publish:"+baseReqBean.getId()+"=====参数存在为空");
        }
        return map;
    }

    @Override
    public Map deleteComment(BaseReqBean baseReqBean) throws Exception {
        Map map = new HashMap();map.put("status", 0);
        if(baseReqBean != null && !StringUtils.isEmpty(baseReqBean.getId()) &&
                !StringUtils.isEmpty(baseReqBean.getUserId())){
            //判断是否拥有删除该信息权限（已经controller层判断）

            //删除评论
            commentDao.deleteByPrimaryKey(baseReqBean.getId());
            //查询该原始信息内容
            map.put("status", 1);
            map.put("message", "删除成功");
        }else {
            map.put("message", "参数值存在为空，请重试。。。");
            logger.error("ERROR=====>publish:"+baseReqBean.getId()+"=====参数存在为空");
        }
        return map;
    }

    @Override
    public Map likeComment(BaseReqBean baseReqBean) throws Exception {
        Map map = new HashMap();map.put("status", 0);
        if (baseReqBean != null && !StringUtils.isEmpty(baseReqBean.getId())
                && !StringUtils.isEmpty(baseReqBean.getUserId())) {
            //点赞
            baseReqBean.setUpdateTime(new Date());
            commentDao.likeComment(baseReqBean);
            //返回该这条评论信息
            Comment comment = new Comment();
            comment.setId(baseReqBean.getId());
            map.put("status", 1);
            map.put("message", commentDao.selectByPrimaryKey(comment));
        }else {
            map.put("message", "参数值存在为空，请重试。。。");
            logger.error("ERROR=====>publish:"+baseReqBean.getId()+"=====参数存在为空");
        }
        return map;
    }

    public static Comment convertToComment(BaseReqBean baseReqBean){
        Comment comment = new Comment();
        comment.setId(UUID.randomUUID().toString().replaceAll("-",""));
        comment.setInfoId(baseReqBean.getId());
        comment.setContent(baseReqBean.getContent());
        comment.setUserId(baseReqBean.getUserId());
//        comment.setParentId(baseReqBean.getCommentId());
        comment.setLike(0);//初始赞为0
        comment.setCreateTime(new Date());
        comment.setContent(baseReqBean.getContent());
        return comment;
    }


}
